package com.CMUBiS.util;

/**
 * A stop watch class to time how long pags take to process
 * TODO: Andy: Check the overhead of the monitoring, whatever the result, see if you can make it lower
 * 
 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
 * @version 0.1
 * @since 0.1
 * @date 04/05/2011
 */
public class StopWatch {
	private long startTime = 0;
	private long stopTime = 0;
	private boolean running = false;
	
	/**
	 * Starts the stop watch
	 * 
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public void start(){
		this.startTime = System.currentTimeMillis();
		running = true;
	}
	
	/**
	 * Stops the stop watch
	 * 
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public void stop(){
		this.stopTime = System.currentTimeMillis();
		running = false;
	}
	
	/**
	 * Returns the amount of elapsed time between the start and now (or the end of the timer)
	 * @return long elapsed time
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public long getTimeTaken(){
		if(running){
			return  System.currentTimeMillis() - startTime;
		}
		else return stopTime - startTime;
	}
}
